package com.md.api.controller;

import com.md.api.annotation.NoRepeatSubmit;
import com.md.api.common.utils.IpUtil;
import com.md.api.common.utils.StringUtils;
import com.md.api.entity.HttpResult;
import com.md.api.entity.TbOrder;
import com.md.api.entity.dto.ProductBuyDto;
import com.md.api.entity.dto.ShopCartBuyDto;
import com.md.api.entity.dto.VipBuyDto;
import com.md.api.entity.util.PayResult;
import com.md.api.service.PayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Slf4j
@RequestMapping("/pay")
@RestController
public class PayController {


	@Resource
	PayService payService;




	@NoRepeatSubmit
	@RequestMapping("/productBuy")
	@ResponseBody
	public HttpResult buy(HttpServletRequest request, @RequestBody ProductBuyDto buyDto) {
		if (null==buyDto.getNumCount()||StringUtils.isEmpty(buyDto.getSkuId())){
			return HttpResult.PARAM_ERROR;
		}
		return payService.productBuy(request,buyDto);
	}
	@RequestMapping("/productNotify")
	public void productNotify(HttpServletRequest request, HttpServletResponse response, PayResult payResult) throws IOException {
		String ip = IpUtil.getRealIp(request);
		log.info("回调请求方ip："+ip);
		log.info("productNotify回调啦："+payResult.toString());
		payService.productNotify(payResult,false);
		if (StringUtils.isNotEmpty(payResult.getMerchant_reference())&&payResult.getMerchant_reference().startsWith("sn")){
			response.sendRedirect("https://kelzproductionhouse.com/#/myAccount");
		}else{
			response.sendRedirect("https://kelzproductionhouse.com:19201/#/pages/index/user");
		}
	}


	//购物车支付
	@NoRepeatSubmit
	@RequestMapping("/shopCartBuy")
	public HttpResult shopCartBuy(HttpServletRequest request, @RequestBody ShopCartBuyDto buyDto) {
		if (CollectionUtils.isEmpty(buyDto.getCartIds())){
			return HttpResult.PARAM_ERROR;
		}
		return payService.shopCartBuy(request,buyDto);
	}
	@RequestMapping("/shopCartNotify")
	public void shopCartNotify(HttpServletRequest request, HttpServletResponse response, PayResult payResult) throws IOException {

		String ip = IpUtil.getRealIp(request);
		log.info("回调请求方ip："+ip);
		log.info("shopCartNotify回调啦："+payResult.toString());
		payService.productNotify(payResult,true);
		if (StringUtils.isNotEmpty(payResult.getMerchant_reference())&&payResult.getMerchant_reference().startsWith("sn")){
			response.sendRedirect("https://kelzproductionhouse.com/#/myAccount");
		}else{
			response.sendRedirect("https://kelzproductionhouse.com:19201/#/pages/index/user");
		}
	}


	//充值
	@NoRepeatSubmit
	@RequestMapping("/recharge")
	public HttpResult recharge(HttpServletRequest request, @RequestBody TbOrder order) {
		if (null==order.getActualPay()||null==order.getTotalPay()||null==order.getPayType()){
			return HttpResult.PARAM_ERROR;
		}
		return payService.recharge(request,order);
	}

	@RequestMapping("/rechargeNotify")
	public void rechargeNotify(HttpServletRequest request,HttpServletResponse response,  PayResult payResult) throws IOException {
		String ip = IpUtil.getRealIp(request);
		log.info("回调请求方ip："+ip);
		log.info("rechargeNotify回调啦："+payResult.toString());
		payService.rechargeNotify(payResult);
		if (StringUtils.isNotEmpty(payResult.getMerchant_reference())&&payResult.getMerchant_reference().startsWith("sn")){
			response.sendRedirect("https://kelzproductionhouse.com/#/myAccount");
		}else{
			response.sendRedirect("https://kelzproductionhouse.com:19201/#/pages/index/user");
		}
	}

	//会员购买
	@NoRepeatSubmit
	@RequestMapping("/getVipPrice")
	public HttpResult getVipPrice(HttpServletRequest request, @RequestBody VipBuyDto vip) {
		if (StringUtils.isEmpty(vip.getServiceCode())){
			return HttpResult.PARAM_ERROR;
		}
		return payService.getVipPrice(request,vip);
	}

	//会员购买
	@NoRepeatSubmit
	@RequestMapping("/vip")
	public HttpResult vip(HttpServletRequest request, @RequestBody VipBuyDto vip) {
		if (StringUtils.isEmpty(vip.getServiceCode())){
			return HttpResult.PARAM_ERROR;
		}
		return payService.vip(request,vip);
	}
	@NoRepeatSubmit
	@RequestMapping("/vipNotify")
	public void vipNotify(HttpServletRequest request, HttpServletResponse response,  PayResult payResult) throws IOException {
		String ip = IpUtil.getRealIp(request);
		log.info("回调请求方ip："+ip);
		log.info("vipNotify回调啦："+payResult.toString());
		payService.vipNotify(payResult);
		if (StringUtils.isNotEmpty(payResult.getMerchant_reference())&&payResult.getMerchant_reference().startsWith("sn")){
			response.sendRedirect("https://kelzproductionhouse.com/#/myAccount");
		}else{
			response.sendRedirect("https://kelzproductionhouse.com:19201/#/pages/index/user");
		}
	}

	//充值记录
//	@RequestMapping("/rechargePage")
//	public HttpResult rechargePage(HttpServletRequest request, @RequestBody ComposeQueryDto composeQueryDto) {
//		return payService.rechargePage(request,composeQueryDto);
//	}
}
